package com.itcoon.cloud.framework.springdoc.plugin;/**
 * @author Athric Yu
 * 2024/11/11
 **/

import com.itcoon.cloud.framework.enums.BasicEnum;
import com.itcoon.cloud.framework.enums.ValueEnum;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.ParameterCustomizer;
import org.springframework.core.MethodParameter;

import java.io.Serializable;
import java.util.List;

/**
 *
 *
 * @author Athric Yu
 * 2024/11/11
 **/
public class BasicEnumParameterCustomizer<V extends Serializable, E extends Enum<E>> implements ParameterCustomizer, BasicEnumCustomizer<V, E> {

    public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
        Class<?> parameterType = methodParameter.getParameterType();
        if(BasicEnum.class.isAssignableFrom(parameterType)){
            Schema<?> originSchema = parameterModel.getSchema();
            String description = getDescription((Class<BasicEnum<V,E>>) parameterType);
            if(originSchema.getDescription() != null){
                description = originSchema.getDescription() + "=" + description;
            }
            parameterModel.setDescription(description);
            Schema<V> schema = new Schema<>();
            List<V> values = getValues((Class<ValueEnum<V, E>>) parameterType);
            schema.setEnum(values);
            if(!values.isEmpty() && parameterModel.getRequired()){
                schema.setExample(values.get(0));
            }
            parameterModel.setSchema(schema);
        }
        return parameterModel;
    }
}
